package com.example.stock.tencent;

import com.example.stock.entity.StockDataResponse;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@Data
public class StockBean {

    // 基本信息
    private String code;    // 代码
    private String name;    // 名称

    // 价格相关
    private double priceNow;     //当前价
    private double priceMax;     //最高价
    private double priceMin;     //最低价
    private double priceAvg;     //均价

    // 成交量与金额
    private long volume;           // 今日累计成交量（手）
    private double turnover;       // 今日累计成交金额（万元）
    private double outPan;         // 外盘
    private double inPan;          //内盘

    // 买卖盘口信息
    private OrderBook buyOrders;   // 买方订单列表
    private OrderBook sellOrders;  // 卖方订单列表

    // 时间戳
    private String calcTime;      // 数据更新的时间，格式为YYYYMMDDHHmmss

    // 涨跌情况
    private double priceChange;   // 相对于昨日收盘价的涨跌额
    private double changePercent;  // 相对于昨日收盘价的涨跌幅百分比


    // 其他统计信息
    private double amplitude;      // 振幅%
    private double peRatioTTM;     // 市盈率TTM
    private double peRatio;        // 市净率
    private double totalMarketCap; // 总市值（亿元）
    private double circulatingCap; // 流通市值（亿元）
    private double dynamicPeRatio; // 动态市盈率
    private double staticPeRatio; // 静态市盈率
    private double volumeCompare;  //量比
    private double exchange; //换手率
    private double highLimit; //涨停价
    private double lowLimit; //跌停价










    private String costPrise;//成本价
    private String bonds;//持仓
    private String incomePercent;//收益率
    private String income;//收益

    private String calcType;//类型


    private StockDataResponse stockData;


    private String plate;

    private Integer score;

    @Data
    // 内部类：单个订单信息
    public static class Order {
        private double price; //价格
        private int quantity; //手数

        public Order(double price, int quantity) {
            this.price = price;
            this.quantity = quantity;
        }
    }

    @Data
    public static class OrderBook {
        private List<Order> orders = new ArrayList<>();

        public void addOrder(Order order) {
            orders.add(order);
        }

        public List<Order> getOrders() {
            return orders;
        }
    }

    public StockBean() {
    }

    //配置code同时配置成本价和成本值
    public StockBean(String code) {
        if (StringUtils.isNotBlank(code)) {
            String[] codeStr = code.split(",");
            if (codeStr.length > 2) {
                this.code = codeStr[0];
                this.costPrise = codeStr[1];
//                this.cost = codeStr[2];
                this.bonds = codeStr[2];
            } else {
                this.code = codeStr[0];
                this.costPrise = "--";
//                this.cost = "--";
                this.bonds = "--";
            }
        } else {
            this.code = code;
        }
        this.name = "--";
    }

    public StockBean(String code, Map<String, String[]> codeMap){
        this.code = code;
        if(codeMap.containsKey(code)){
            String[] codeStr = codeMap.get(code);
            if (codeStr.length > 2) {
                this.code = codeStr[0];
                this.costPrise = codeStr[1];
//                this.cost = codeStr[2];
                this.bonds = codeStr[2];
            }
        }
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        StockBean bean = (StockBean) o;
        return Objects.equals(code, bean.code);
    }

    @Override
    public int hashCode() {
        return Objects.hash(code);
    }


//    /**
//     * 返回列名的VALUE 用作展示
//     *
//     * @param colums   字段名
//     * @param colorful 隐蔽模式
//     * @return 对应列名的VALUE值 无法匹配返回""
//     */
//    public String getValueByColumn(String colums, boolean colorful) {
//        switch (colums) {
//            case "编码":
//                return this.getCode();
//            case "股票名称":
//                return colorful ? this.getName() : PinYinUtils.toPinYin(this.getName());
//            case "当前价":
//                return this.getNow();
//            case "涨跌":
//                return this.getChange() + "";
//            case "涨跌幅":
//                return this.getChangePercent() + "%";
//            case "最高价":
//                return this.getMax();
//            case "最低价":
//                return this.getMin();
//            case "成本价":
//                return this.getCostPrise();
//            case "持仓":
//                return this.getBonds();
//            case "收益率":
//                return this.getCostPrise() != null ? this.getIncomePercent() + "%" : this.getIncomePercent();
//            case "收益":
//                return this.getIncome();
//            case "更新时间":
//                String timeStr = "--";
//                if (this.getTime() != null) {
//                    timeStr = this.getTime().substring(8);
//                }
//                return timeStr;
//            default:
//                return "";
//
//        }
//    }
}
